﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MadLibrary.Text
{
    /// <summary>
    /// Regex for Chinese Characters
    /// </summary>
    public class ChineseRegex
    {
        /*
         * The unicode range for chinese characters is：0x4E00~0x9FA5
         */
        private static string chineseCharacterPattern = "[\u4e00-\u9fa5]";
        private static string chineseStringPattern = "[\u4e00-\u9fa5]+";

        /// <summary>Query if 'input' is match.</summary>
        /// <param name="input">.</param>
        /// <returns>true if match, false if not.</returns>
        public static bool IsMatch(char input)
        {
            return Regex.IsMatch(input.ToString(), chineseCharacterPattern);
        }

        /// <summary>Query if 'input' is match.</summary>
        /// <param name="input">.</param>
        /// <returns>true if match, false if not.</returns>
        public static bool IsMatch(string input)
        {
            for (int i = 0; i < input.Length; i++)
            {
                if (!Regex.IsMatch(input[i].ToString(), chineseCharacterPattern))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>Matches.</summary>
        /// <param name="input">.</param>
        /// <returns>.</returns>
        public static Match Match(string input)
        {
            return Regex.Match(input, chineseCharacterPattern);
        }

        /// <summary>Match string.</summary>
        /// <param name="input">.</param>
        /// <returns>.</returns>
        public static Match MatchString(string input)
        {
            return Regex.Match(input, chineseStringPattern);
        }

        /// <summary>Matches.</summary>
        /// <param name="input">.</param>
        /// <returns>.</returns>
        public static MatchCollection Matches(string input)
        {
            return Regex.Matches(input, chineseCharacterPattern);
        }

        /// <summary>Match strings.</summary>
        /// <param name="input">.</param>
        /// <returns>.</returns>
        public static List<Match> MatchStrings(string input)
        {
            List<Match> matches = new List<Match>();
            RecursiveMatchString(input, matches);
            return matches;
        }

        #region

        /// <summary>
        /// Get the chinese matches
        /// </summary>
        /// <param name="input"></param>
        /// <param name="matches"></param>
        private static void RecursiveMatchString(string input, List<Match> matches)
        {
            Match match = MatchString(input);
            if (match.Value.Length > 0)
            {
                matches.Add(match);
                int matchIndex = input.IndexOf(match.Value);
                input = input.Substring(matchIndex + match.Value.Length);
                RecursiveMatchString(input, matches);
            }
        }

        #endregion
    }
}